package com.wt.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wt.controller.LoginController;
import com.wt.entity.WqunAccount;
import com.wt.entity.utilEntity.login.LoginAccount;
import com.wt.entity.utilEntity.login.Register;

import com.wt.util.R.R;
import com.wt.util.ResultMess.ResultMess;
import com.wt.util.axios.Axios;
import com.wt.util.axios.JsonToEntity;
import com.wt.util.axios.Return;
import com.wt.util.exceptionhandler.WQUNException;
import org.apache.commons.beanutils.BeanUtils;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;


@WebServlet({"/testLoginController/login","/loginOut","/testLoginController/register","/checkCode"})
public class LoginServlet extends HttpServlet {
    LoginController loginController = new LoginController();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JSONObject jsonObject = null;
        HashMap<String, String> map = null;

        //System.out.println(req.getRequestURL().substring(req.getRequestURL().lastIndexOf("/")+1));
        switch (req.getRequestURL().substring(req.getRequestURL().lastIndexOf("/")+1)){
            case "login":
                try {
                    jsonObject = Axios.post(req);//Json接受
                } catch (IOException e) {
                    Return.R(resp,R.error().message(ResultMess.DARA_FORMAT_ERROR.getDesc()));
                }
                try {
                    map = JsonToEntity.LoginAccount(jsonObject);//将json转换成Object
                } catch (WQUNException e) {
                    Return.R(resp,R.error().message(e.getMsg()));//异常返回
                }
                LoginAccount loginAccount = new LoginAccount();
                try {
                    BeanUtils.populate(loginAccount, map);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new RuntimeException(e);
                }
                Return.R(resp,loginController.login(loginAccount));



                break;
            case "loginOut":

                break;
            case "register":
                try {
                    jsonObject = Axios.post(req);//Json接受
                } catch (IOException e) {
                    Return.R(resp,R.error().message(ResultMess.DARA_FORMAT_ERROR.getDesc()));
                }
                try {
                    map = JsonToEntity.wqunAccount(jsonObject);//将json转换成Object
                } catch (WQUNException e) {
                    Return.R(resp,R.error().message(e.getMsg()));//异常返回
                }
                WqunAccount wqunAccount = new WqunAccount();
                try {
                    BeanUtils.populate(wqunAccount, map);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new RuntimeException(e);
                }
                Return.R(resp,loginController.register(wqunAccount));
                break;
            case "checkCode":

        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
